<?php

/**
 * This file is part of the Pagizer package.
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 *
 * @copyright   Copyright (c) 2010 Advisa (http://www.advisa.fr)
 * @author      Pagizer Core Team <team@pagizer.org>
 * @package     pagizer
 * @subpackage  medias
 */

class m_medias_actions_block_FormAddParameterAction extends m_backoffice_extends_FormModal
{
	public function execute()
	{
		$formProvider = f_form_Provider::getInstance();
		
		$formProvider->setDocumentName("addParameter")
		             ->setModuleName("medias")
		             ->setFormName("FormAddParameter");

        $values = $formProvider->getParams();

        $folder = f_document_Provider::getInstance()->getByUniqueId($this->getRequestParameter("folderId"), $this->getRequestParameter("folderLang"));
		$this->addToModel("folder", $folder);
		$this->addToModel("href", $this->getRequestParameter("href"));

		if($formProvider->isSubmitted())
		{
            $values = $values['formValues'];
            
			if($formProvider->isValid())
			{								
				if(!is_null($this->getRequestParameter("docId")))
				{
					$parameter = f_document_Provider::getInstance()->getByModelAndUniqueId('medias/Parameter', $this->getRequestParameter("docId"),$folder->getLang());
				}
				else
				{
					$parameter = m_medias_documents_Parameter::createNew();					
				}
				
				$values["generateOnUpload"] == "true" ? $generateOnUpload = true : $generateOnUpload = false;
				
				$parameter	->setWidth($values["width"])
							->setHeight($values["height"])
							->setFolderId($folder->getUniqueId())
							->setGenerateOnUpload($generateOnUpload)
                            ->setCrop($values["crop"])
							->setLabel($values["label"])
							->save();
							
				$folder->addParameter($parameter)->save();
				
				$url = f_core_Routing::getUrl("mediasListTab", array("docId" => $folder->getUniqueId(), "docLang" => strtolower($folder->getLang()), "tabName" => "parameters"));

				$this->addToModel("href", $url);								
				$errors["status"] = "success";							
							
				$this->addToModel("errors", $errors);
			}
			else
			{
				$this->addToModel("errors", $formProvider->getErrors());
			}
		}
		else
		{
			$parameter = f_document_Provider::getInstance()->getByModelAndUniqueId('medias/Parameter', $this->getRequestParameter("docId"),$folder->getLang());
			$values["docId"] = $parameter->getUniqueId();
			$values["label"] = $parameter->getLabel();
			$values["width"] = $parameter->getWidth();
			$values["height"] = $parameter->getHeight();
            $values["crop"] = is_null($parameter->getCrop()) ? 1 : $parameter->getCrop();
			$values["generateOnUpload"] = $parameter->isGenerateOnUpload();
		}
        
        $values["cropLabel"] = m_medias_services_ParameterService::getInstance()->getResizeModeLabel();
        $this->addToModel("values", $values);
		$this->setView('success');
	}		
}	